package com.pushbox.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pushbox.common.SessionName;
import com.pushbox.model.UserLogin;
import com.pushbox.service.LoginService;
import com.pushbox.service.UserService;
import com.pushbox.vo.BaseResult;

/**
 * Created by Hatcher on 2016/9/24 0024.
 */
@Controller
public class LoginController {

	@Autowired
	private LoginService loginService;
	@Autowired
	private UserService userService;

	/**
	 * 登录接口
	 * 
	 * @param userLogin
	 * @param session
	 * @return
	 */
	@RequestMapping("isLogin")
	@ResponseBody
	public BaseResult isLogin(UserLogin userLogin, HttpSession session) {
		BaseResult result = new BaseResult();
		Integer userId = loginService.isLogin(userLogin);
		if (userId != null) {
			session.setAttribute(SessionName.USER_ID, userId);
			result.setResultMsg("登录成功！");
			return result;
		} else {
			result.setResult(false);
			result.setResultMsg("登录失败！");
			return result;
		}
	}

	/**
	 * 注册接口
	 * 
	 * @param userLogin
	 * @return
	 */
	@RequestMapping("register")
	@ResponseBody
	public BaseResult register(UserLogin userLogin) {
		BaseResult result = new BaseResult();
		try {
			int value = userService.addUser(userLogin);
			if (1 != value) {
				result.setResult(false);
			}else{
				result.setResultMsg("注册成功！");
			}
		} catch (Exception e) {
			result.setResult(false);
			result.setResultMsg("注册失败！用户名已被使用，请更换用户名重新注册！");
		}
		return result;
	}

	// 模板
	// @RequestMapping
	// @ResponseBody
	// public BaseResult register(UserLogin userLogin) {
	// BaseResult result = new BaseResult();
	// return result;
	// }
}
